package com.yonyou.le.unattended.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.format.datetime.DateFormatter;

import com.yonyou.le.unattended.delivery.enumeration.DevicePositionEnum;
import com.yonyou.le.unattended.delivery.enumeration.DevtypeEnum;

/**
 * 工具类
 * @author zhangbbd
 **/
public class CommonUtils {
	private static DateFormatter df=new DateFormatter("yyyy-MM-dd HH:mm:ss");
	
	/**
     * 格式化日期
     * @param d
     * @return
     */
    public static String formatDate(Date d){
		return df.print(d, Locale.CHINESE);    	
    }
    /**
     * 构建消息，带日期
     * @param msg
     * @return
     */
    public static String buildDateMessage(String msg){
    	StringBuilder sb = new StringBuilder(df.print(new Date(), Locale.CHINESE));
    	return sb.append(" ").append(msg).toString();
    }
    /**
     * 返回编码数组
     * @param str
     * @return
     */
    public static String[] getCodeArray(String str){
    	if(str == null) return null;
    	if(str.indexOf("-")!=-1){
    		return str.split("-");
    	}else{
    		return new String[]{str};
    	}
    }
    
    private static Logger logger = LoggerFactory.getLogger(CommonUtils.class);
	/**
	 * 将List 根据属性转为Map<String,T>
	 * @param list
	 * @param attrName
	 * @return
	 */
	public static <T> Map<String,T> list2Map(List<T> list,String attrName) {
		if(list == null){
			return null;
		}
		Map<String,T> map =new HashMap<String, T>();
		try{
			for(T o:list){
				if(o==null)
					continue;
				Field field = o.getClass().getDeclaredField(attrName);
				field.setAccessible(true);
				String att=(String) field.get(o);
				if(att==null){
					continue;
				}else if(map.containsKey(att)){
					logger.warn(o.getClass().getSimpleName()+"@List"+"的KEY["+attrName+"]重复:"+att);
				}else{
					map.put(att, o);
				}				
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		return map;
	}
	/**
	 * 将List 根据属性转为Map<String,T>
	 * @param list
	 * @param attrName
	 * @return
	 */
	public static <T> Map<String,List<T>> list2Maplist(List<T> list,String attrName) {
		if(list == null){
			return null;
		}
		Map<String,List<T>> map =new HashMap<String, List<T>>();
		try{
			for(T o:list){
				if(o==null)
					continue;
				Field field = o.getClass().getDeclaredField(attrName);
				field.setAccessible(true);
				String att=(String) field.get(o);
				if(att==null){
					continue;
				}else if(map.containsKey(att)){
					List<T> l = map.get(att);
					l.add(o);
				}else{
					List<T> l = new ArrayList<T>();
					l.add(o);
					map.put(att, l);
				}				
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return map;
	}
	
	public static <T> List<T> sortListByAttr(String attrName,List<T> list){
		Collections.sort(list, new ObjComparator<T>(attrName));
		return list;
		
	}
	
//	public static void main(String[] args) {
//		String s="a";
//		System.out.println(getCodeArray(s).length);
//	}
}
class ObjComparator<T> implements Comparator<T>{
	private String attrName;
	public ObjComparator(String attrName) {
		this.attrName = attrName;
	}
	@Override
	public int compare(T o1, T o2) {
		Class<?> clazz = o1.getClass();
		try {
			Field field = clazz.getDeclaredField(attrName);
			field.setAccessible(true);
			String att1=(String) field.get(o1);
			String att2=(String) field.get(o2);
			return att1.compareTo(att2);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return 0;
	}


	
}
